﻿using UnityEngine;
using System.Collections;

namespace AutoRoot
{
   public class TouchedObjHandler
   {
      public void handleClickEvent(GameObject obj, Vector3 hitPoint)
      {
         if (Input.GetMouseButtonDown(0))
         {
            OnTouchDown(obj, hitPoint);
         }
         else if (Input.GetMouseButtonUp(0))
         {
            OnTouchUp(obj, hitPoint);
         }
         else if (Input.GetMouseButton(0))
         {
            OnTouchStay(obj, hitPoint);
         }
      }

      public void handleTouchEvent(GameObject obj, Vector3 hitPoint, Touch touch)
      {
         if (touch.phase == TouchPhase.Began)
         {
            OnTouchDown(obj, hitPoint);
         }
         else if (touch.phase == TouchPhase.Ended)
         {
            OnTouchUp(obj, hitPoint);
         }
         else if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
         {
            OnTouchStay(obj, hitPoint);
         }
         else if (touch.phase == TouchPhase.Canceled)
         {
            OnTouchExit(obj, hitPoint);
         }
      }

      protected virtual void OnTouchDown(GameObject obj, Vector3 hitPoint)
      {
         obj.SendMessage("OnTouchDown", hitPoint, SendMessageOptions.DontRequireReceiver);
      }
      protected virtual void OnTouchUp(GameObject obj, Vector3 hitPoint)
      {
         obj.SendMessage("OnTouchUp", hitPoint, SendMessageOptions.DontRequireReceiver);
      }
      protected virtual void OnTouchStay(GameObject obj, Vector3 hitPoint)
      {
         obj.SendMessage("OnTouchStay", hitPoint, SendMessageOptions.DontRequireReceiver);
      }
      protected virtual void OnTouchExit(GameObject obj, Vector3 hitPoint)
      {
         obj.SendMessage("OnTouchExit", hitPoint, SendMessageOptions.DontRequireReceiver);
      }
   }
}